layui.define(function(exports){
    var $ = layui.$
        , layer = layui.layer
        , laytpl = layui.laytpl
        , setter = layui.setter
        , view = layui.view
        , admin = layui.admin
        , table = layui.table
        , upload = layui.upload
        , element = layui.element;

    //公共业务的逻辑处理可以写在此处，切换任何页面都会执行
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    //清楚缓存
    admin.events.clear = function(){
        //执行退出接口
        admin.req({
            url: '/home/clear'
            ,type: 'get'
            ,data: {}
            ,done: function(res){ //这里要说明一下：done 是只有 response 的 code 正常才会执行。而 succese 则是只要 http 为 200 就会执行
                layer.msg(res.msg, {icon: 6});
            }
        });
    };

    //退出
    admin.events.logout = function(){
        //执行退出接口
        admin.req({
            url: '/logout'
            ,type: 'post'
            ,error: function(res){ //这里要说明一下：done 是只有 response 的 code 正常才会执行。而 succese 则是只要 http 为 200 就会执行
                //清空本地记录的 token，并跳转到登入页
                admin.exit(function(){
                    location.href = '/login';
                });
            }
        });
    };

    //查看图片
    admin.events.image_preview = function (othis) {
        layer.photos({
            photos: {
                "title": "查看图片" //相册标题
                , "data": [{
                    "src": othis.attr('src') //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    //图片管理
    table.render({
        elem: '#LAY-app-attachment-image'
        , url: '/attachment?file_type=image'
        , cols: [[
            {field: 'file_path', title: '图片预览', width: 100, templet: '#imageTpl'}
            , {field: 'file_name', title: '图片名称'}
            , {field: 'file_size', width: 90, title: '图片大小'}
            , {field: 'create_time', width: 160, title: '上传时间'}
            , {title: '操作', width: 100, align: 'center', fixed: 'right', toolbar: '#table-file-list'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });



    //上传图片
    const uploadInst = upload.render({
        elem: '#LAY-upload-image'
        , url: setter.host + '/attachment'
        , accept: 'images'
        , method: 'post'
        , acceptMime: 'image/*'
        , multiple: true
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#LAY-upload-image-preview').append('<img src="' + result + '" alt="' + file.name + '" style="height: 38px;">')
            });
        }
        ,progress: function(n){
            element.progress('progress', n + '%');
        }
        , done: function (res) {
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            layui.table.reload('LAY-app-attachment-image'); //重载表格
        }
        , error: function () {
            const normal = $('#LAY-upload-image-normal');
            normal.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini image-reload">重试</a>');
            normal.find('.image-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });

    //选择图片
    admin.events.select_image = function (othis) {
        layer.open({
            type: 2
            , title: '选择图片'
            , content: '/attachment?file_type=image'
            , area: ['800px', '550px']
            , success: function (layero, index) {
                var iframeWindow = window['layui-layer-iframe' + index]

                //监听工具条
                iframeWindow.layui.table.on('tool(LAY-app-attachment-image)', function (obj) {
                    if (obj.event === 'select') {
                        $(othis).prev("div").children("input").val(obj.data.file_path)
                        $(othis).next().remove();
                        $(othis).after('<div><img src="' + obj.data.file_path + '" alt="' + obj.data.file_name +
                            '" style="margin-left:5px;height: 38px;max-width: 38px;"></div>')
                        layer.close(index);
                    }
                });
            }
        });
    };

    //选择文件
    admin.events.select_file = function (othis) {
        admin.popup({
            title: '选择文件'
            , area: ['800px', '550px']
            , id: 'LAY-app-attachment-file-select'
            , success: function (layero, index) {
                view(this.id).render('app/attachment/file').done(function () {
                    //图片管理
                    table.render({
                        elem: '#LAY-app-attachment-file'
                        , url: setter.host + '/attachment?file_type=file'
                        , cols: [[
                            {field: 'file_path', title: '文件预览', width: 100, templet: '#imgTpl'}
                            , {field: 'file_name', title: '文件名称'}
                            , {field: 'file_size', width: 90, title: '文件大小'}
                            , {field: 'create_time', width: 160, title: '上传时间'}
                            , {title: '操作', width: 100, align: 'center', fixed: 'right', toolbar: '#table-file-list'}
                        ]]
                        , page: true
                        , limit: 10
                        , limits: [10, 15, 20, 25, 30]
                        , text: '对不起，加载出现异常！'
                    });

                    //监听工具条
                    table.on('tool(LAY-app-attachment-file)', function (obj) {
                        if (obj.event === 'select') {
                            $(othis).prev("div").children("input").val(obj.data.file_path)
                            layer.close(index);
                        }
                    });

                    //上传
                    const uploadInst = upload.render({
                        elem: '#LAY-upload-file'
                        , url: setter.host + '/attachment?file_type=file'
                        , accept: 'file'
                        , method: 'post'
                        ,progress: function(n){
                            element.progress('progress', n + '%');
                        }
                        , done: function (res) {
                            if (res.code > 0) {
                                return layer.msg('上传失败');
                            }
                            layui.table.reload('LAY-app-attachment-file'); //重载表格
                        }
                        , error: function () {
                            const normal = $('#LAY-upload-video-normal');
                            normal.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini image-reload">重试</a>');
                            normal.find('.image-reload').on('click', function () {
                                uploadInst.upload();
                            });
                        }
                    });
                });
            }
        });
    };

    //选择视频
    admin.events.select_video = function (othis) {
        admin.popup({
            title: '选择视频'
            , area: ['800px', '550px']
            , id: 'LAY-app-attachment-video-select'
            , success: function (layero, index) {
                view(this.id).render('app/attachment/video').done(function () {
                    //图片管理
                    table.render({
                        elem: '#LAY-app-attachment-video'
                        , url: setter.host + '/attachment?file_type=video'
                        , cols: [[
                            {field: 'file_path', title: '视频预览', width: 100, templet: '#imgTpl'}
                            , {field: 'file_name', title: '视频名称'}
                            , {field: 'file_size', width: 90, title: '视频大小'}
                            , {field: 'create_time', width: 160, title: '上传时间'}
                            , {title: '操作', width: 100, align: 'center', fixed: 'right', toolbar: '#table-file-list'}
                        ]]
                        , page: true
                        , limit: 10
                        , limits: [10, 15, 20, 25, 30]
                        , text: '对不起，加载出现异常！'
                    });

                    //监听工具条
                    table.on('tool(LAY-app-attachment-video)', function (obj) {
                        if (obj.event === 'select') {
                            $(othis).prev("div").children("input").val(obj.data.file_path)
                            layer.close(index);
                        }
                    });

                    //上传文件
                    const uploadInst = upload.render({
                        elem: '#LAY-upload-video'
                        , url: setter.host + '/attachment?file_type=video'
                        , accept: 'file'
                        , method: 'post'
                        ,progress: function(n){
                            element.progress('progress', n + '%');
                        }
                        , done: function (res) {
                            if (res.code > 0) {
                                return layer.msg('上传失败');
                            }
                            layui.table.reload('LAY-app-attachment-video'); //重载表格
                        }
                        , error: function () {
                            const normal = $('#LAY-upload-video-normal');
                            normal.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini image-reload">重试</a>');
                            normal.find('.image-reload').on('click', function () {
                                uploadInst.upload();
                            });
                        }
                    });
                });
            }
        });
    };

    //对外暴露的接口
    exports('common', {});
});
